// WebPage.cpp: implementation of the CWebPage class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "AUSlotsTester.h"
#include "WebPage.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CWebPage::CWebPage()
{
	m_bDirty = true;
}

CWebPage::~CWebPage()
{
}

bool CWebPage::IsDirty() const
{
	return m_bDirty;
}

void CWebPage::SetDirty( bool bDirty )
{
	m_bDirty = bDirty;
}

bool CWebPage::GetFileContents( LPCSTR* ppszContentType, void** ppFileContents, UINT* pnFileSize ) const
{
	CWebPage* pThis = const_cast< CWebPage* >( this );

	if ( IsDirty() )
	{
		pThis->m_strFileContents.Empty();
		pThis->GenerateContent();
		pThis->SetDirty( false );
	}

	*ppszContentType = "text/html";

	*ppFileContents = pThis->m_strFileContents.GetBuffer(0);

	*pnFileSize = m_strFileContents.GetLength();

	return true;
}

void CWebPage::ResetContent()
{
	m_strFileContents.Empty();
}

const CString& CWebPage::GetFileContents() const
{
	return m_strFileContents;
}

void CWebPage::InsertAlignmentTag( HORIZONTAL_ALIGNMENT ha )
{
	CString str;

	switch ( ha )
	{
		case haLeft:
		{
			str = _T( "left" );
		}
		break;

		case haRight:
		{
			str = _T( "right" );
		}
		break;
		
		case haCenter:
		{
			str = _T( "center" );
		}
		break;

		case haJustify:
		{
			str = _T( "justify" );
		}
		break;
	}

	if ( !str.IsEmpty() )
	{
		m_strFileContents += _T( " align=\"" );
		m_strFileContents += str;
		m_strFileContents += _T( "\"" );
	}
}

void CWebPage::InsertAlignmentTag( VERTICAL_ALIGNMENT va )
{
	CString str;

	switch ( va )
	{
		case vaTop:
		{
			str = _T( "top" );
		}
		break;

		case vaMiddle:
		{
			str = _T( "middle" );
		}
		break;
		
		case vaBaseline:
		{
			str = _T( "baseline" );
		}
		break;

		case vaBottom:
		{
			str = _T( "bottom" );
		}
		break;
	}

	if ( !str.IsEmpty() )
	{
		m_strFileContents += _T( " valign=\"" );
		m_strFileContents += str;
		m_strFileContents += _T( "\"" );
	}
}

void CWebPage::InsertColorTag( LPCTSTR szTagName, COLORREF cr )
{
	TCHAR szColor[ 64 ];

	m_strFileContents += _T( " " );
	m_strFileContents += szTagName;
	m_strFileContents += _T( "=\"#" );

	_itot_s( GetRValue( cr ), szColor, 16 );

	if ( szColor[1] == 0 )
	{
		m_strFileContents += _T( "0" );
	}

	m_strFileContents += szColor;

	_itot_s( GetGValue( cr ), szColor, 16 );
	
	if ( szColor[1] == 0 )
	{
		m_strFileContents += _T( "0" );
	}

	m_strFileContents += szColor;

	_itot_s( GetBValue( cr ), szColor, 16 );

	if ( szColor[1] == 0 )
	{
		m_strFileContents += _T( "0" );
	}

	m_strFileContents += szColor;

	m_strFileContents += _T( "\"" );
}

void CWebPage::InsertNumberString( TCHAR* p, int nLength )
{
	if ( nLength == 0 )
	{
		return;
	}

	if ( *p == _T( '-' ) )
	{
		m_strFileContents += *p;

		nLength--;

		p++;
	}

	for ( int n = 0; n < nLength; n++ )
	{
		if ( n > 0 )
		{
			if ( ( nLength - n ) % 3 == 0 )
			{
				m_strFileContents += _T( ',' );
			}
		}

		m_strFileContents += *p;

		p++;
	}
}

void CWebPage::BeginDocument()
{
	m_strFileContents += _T( "<HTML>" );
}

void CWebPage::EndDocument()
{
	m_strFileContents += _T( "</HTML>" );
}

void CWebPage::BeginHead()
{
	m_strFileContents += _T( "<HEAD>" );
}

void CWebPage::EndHead()
{
	m_strFileContents += _T( "</HEAD>" );
}

void CWebPage::BeginTitle()
{
	m_strFileContents += _T( "<TITLE>" );
}

void CWebPage::EndTitle()
{
	m_strFileContents += _T( "</TITLE>" );
}

void CWebPage::BeginBody()
{
	m_strFileContents += _T( "<BODY>" );
}

void CWebPage::EndBody()
{
	m_strFileContents += _T( "</BODY>" );
}

void CWebPage::BeginHeading( int nType, HORIZONTAL_ALIGNMENT ha /*= haDefault*/ )
{
	TCHAR szType[ 64 ];
	
	_itot_s( nType, szType, 10 );
	
	m_strFileContents += _T( "<H" );
	m_strFileContents += szType;
	InsertAlignmentTag( ha );
	m_strFileContents += _T( ">" );
}

void CWebPage::EndHeading( int nType )
{
	TCHAR szType[ 64 ];
	
	_itot_s( nType, szType, 10 );
	
	m_strFileContents += _T( "</H" );
	m_strFileContents += szType;
	m_strFileContents += _T( ">" );
}

void CWebPage::BeginTable( int nWidth /*= -1*/, bool bWidthIsPercent /*= false*/, int nCellSpacing /*= -1*/, HORIZONTAL_ALIGNMENT ha /*= haDefault*/, VERTICAL_ALIGNMENT va /*= vaDefault*/ )
{
	m_strFileContents += _T( "<TABLE" );

	if ( nWidth != -1 )
	{
		TCHAR szWidth[ 64 ];
		
		_itot_s( nWidth, szWidth, 10 );

		m_strFileContents += _T( " width=\"" );
		m_strFileContents += szWidth;

		if ( bWidthIsPercent )
		{
			m_strFileContents += _T( "%" );
		}

		m_strFileContents += _T( "\"" );
	}

	InsertAlignmentTag( ha );

	InsertAlignmentTag( va );

	if ( nCellSpacing != -1 )
	{
		TCHAR szCellSpacing[ 64 ];
		
		_itot_s( nCellSpacing, szCellSpacing, 10 );

		m_strFileContents += _T( " cellspacing=\"" );
		m_strFileContents += szCellSpacing;
		m_strFileContents += _T( "\"" );
	}

	m_strFileContents += _T( ">" );
}

void CWebPage::EndTable()
{
	m_strFileContents += _T( "</TABLE>" );
}

void CWebPage::BeginRow()
{
	m_strFileContents += _T( "<TR>" );
}

void CWebPage::EndRow()
{
	m_strFileContents += _T( "</TR>" );
}

void CWebPage::BeginColumn( int nWidth, bool bWidthIsPercent, int nHeight, bool bHeightIsPercent, HORIZONTAL_ALIGNMENT ha, VERTICAL_ALIGNMENT va, int nColSpan, COLORREF crBackground )
{
	m_strFileContents += _T( "<TD" );

	if ( nWidth != -1 )
	{
		TCHAR szWidth[ 64 ];
		
		_itot_s( nWidth, szWidth, 10 );

		m_strFileContents += _T( " width=\"" );
		m_strFileContents += szWidth;

		if ( bWidthIsPercent )
		{
			m_strFileContents += _T( "%" );
		}

		m_strFileContents += _T( "\"" );
	}

	if ( nHeight != -1 )
	{
		TCHAR szHeight[ 64 ];
		
		_itot_s( nHeight, szHeight, 10 );

		m_strFileContents += _T( " height=\"" );
		m_strFileContents += szHeight;

		if ( bHeightIsPercent )
		{
			m_strFileContents += _T( "%" );
		}

		m_strFileContents += _T( "\"" );
	}

	InsertAlignmentTag( ha );

	InsertAlignmentTag( va );

	if ( nColSpan != -1 )
	{
		TCHAR szColSpan[ 64 ];
		
		_itot_s( nColSpan, szColSpan, 10 );

		m_strFileContents += _T( " colspan=\"" );
		m_strFileContents += szColSpan;
		m_strFileContents += _T( "\"" );
	}

	if ( crBackground != crWhite )
	{
		InsertColorTag( _T( "bgcolor" ), crBackground );
	}

	m_strFileContents += _T( ">" );
}

void CWebPage::EndColumn()
{
	m_strFileContents += _T( "</TD>" );
}

void CWebPage::BeginAnchor( LPCTSTR szHREF )
{
	m_strFileContents += _T( "<A HREF=" );
	m_strFileContents += szHREF;
	m_strFileContents += _T( ">" );
}

void CWebPage::EndAnchor()
{
	m_strFileContents += _T( "</A>" );
}

void CWebPage::BeginParagraph( HORIZONTAL_ALIGNMENT ha /*= haDefault*/ )
{
	m_strFileContents += _T( "<P" );

	InsertAlignmentTag( ha );

	m_strFileContents += _T( ">" );
}

void CWebPage::EndParagraph()
{
	m_strFileContents += _T( "</P>" );
}

void CWebPage::BeginFont( COLORREF crText )
{
	m_strFileContents += _T( "<FONT" );

	if ( crText != crBlack )
	{
		InsertColorTag( _T( "color" ), crText );
	}

	m_strFileContents += _T( ">" );
}

void CWebPage::EndFont()
{
	m_strFileContents += _T( "</FONT>" );
}

void CWebPage::BeginBold()
{
	m_strFileContents += _T( "<B>" );
}

void CWebPage::EndBold()
{
	m_strFileContents += _T( "</B>" );
}

void CWebPage::InsertContent( LPCTSTR szText )
{
	m_strFileContents += szText;
}

void CWebPage::InsertContent( double d, int nDecimalPlaces )
{
	int nDecimalPoint = 0;
	int nNegative = 0;

	char szNumber[ _CVTBUFSIZE ];
	
	_fcvt_s( szNumber, sizeof(szNumber), d, nDecimalPlaces, &nDecimalPoint, &nNegative );

	if ( nNegative )
	{
		m_strFileContents += _T( '-' );
	}

	if ( nDecimalPoint < 1 )
	{
		m_strFileContents += _T( '0' );
		m_strFileContents += _T( '.' );

		while ( nDecimalPoint < 0 )
		{
			nDecimalPoint++;
			m_strFileContents += _T( '0' );
		}
	}
	else
	{
		InsertNumberString( szNumber, nDecimalPoint );

		m_strFileContents += _T( '.' );
	}

	m_strFileContents += szNumber + nDecimalPoint;
}

void CWebPage::InsertContent( int n )
{
	TCHAR szNumber[ 64 ];
	
	_itot_s( n, szNumber, 10 );

	InsertNumberString( szNumber, _tcslen( szNumber ) );
}

void CWebPage::InsertContent( UINT n )
{
	TCHAR szNumber[ 64 ];
	
	_ultot_s( n, szNumber, 10 );

	InsertNumberString( szNumber, _tcslen( szNumber ) );
}

void CWebPage::InsertContent( DWORD n )
{
	InsertContent( (__int64) n );
}

void CWebPage::InsertContent( __int64 n )
{
	TCHAR szNumber[ _CVTBUFSIZE ];
	
	_i64tot_s( n, szNumber, sizeof( szNumber ), 10 );

	InsertNumberString( szNumber, _tcslen( szNumber ) );
}
